//
//  BLinkEmptyView.swift
//  live
//
//  Created by melvyn on 2018/8/23.
//  Copyright © 2018 NEET. All rights reserved.
//

import UIKit

struct BLinkEmptyConfiguration {
    
    // Display
    var image: UIImage? = UIImage(named: "live_fansempty_ico_130x137_")
    var animationImages: [UIImage]?
    var text: String = "什么都没找到啊T T"
    var textColor: UIColor = #colorLiteral(red: 0.7404708266, green: 0.7404883504, blue: 0.7404789329, alpha: 1)
    var font: UIFont = UIFont.preferredFont(forTextStyle: .body)
    
    // Use `attributedText` will ignore properties of `text`, `textColor`, `font`
    var attributedText: NSAttributedString? = nil
    
    // Layout
    // If size equals to .zero, BLinkEmptyView will automatically adjust size for subviews layout
    var size: CGSize = .zero
    var alignment: UIStackView.Alignment = .center
    var axis: NSLayoutConstraint.Axis = .vertical
    var spacing: CGFloat = UIStackView.spacingUseDefault
    var animationDuration: TimeInterval = 0.8
    var animationRepeatCount: Int = .max
    
    static var `default`: BLinkEmptyConfiguration = {
        let configuration = BLinkEmptyConfiguration.init()
        return configuration
    }()
}

class BLinkEmptyView: UIView {
    
    var imageView: UIImageView = UIImageView()
    var textLabel: UILabel = UILabel()
    var configuration: BLinkEmptyConfiguration = BLinkEmptyConfiguration.default
    
    private var stackView: UIStackView = UIStackView.init(frame: .zero)
    
    convenience init(configuration: BLinkEmptyConfiguration) {
        self.init(frame: .zero)
        self.configuration = configuration
        initialize()
    }
    
    override init(frame: CGRect) {
        super.init(frame: .zero)
        
        initialize()
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        
        initialize()
    }
    
    override func willMove(toWindow newWindow: UIWindow?) {
        super.willMove(toWindow: newWindow)
        
        // Resume animation
        guard newWindow != nil, !imageView.isAnimating, imageView.animationImages?.count ?? 0 > 0 else {
            return
        }
        imageView.startAnimating()
    }
    
    func initialize() {
        stackView.axis = configuration.axis
        stackView.alignment = configuration.alignment
        stackView.spacing = configuration.spacing
        stackView.translatesAutoresizingMaskIntoConstraints = false
        
        imageView.contentMode = .scaleAspectFit
        imageView.image = configuration.image
        imageView.animationImages = configuration.animationImages
        imageView.animationDuration = configuration.animationDuration
        imageView.animationRepeatCount = configuration.animationRepeatCount
        
        if let attributedText = configuration.attributedText {
            textLabel.attributedText = attributedText
        } else {
            let attributedText = NSAttributedString.init(string: configuration.text, attributes: [.font : configuration.font, .foregroundColor : configuration.textColor])
            textLabel.attributedText = attributedText
        }
        
        stackView.addArrangedSubview(imageView)
        stackView.addArrangedSubview(textLabel)
        
        addSubview(stackView)
        
        NSLayoutConstraint.activate([
            stackView.centerXAnchor.constraint(equalTo: centerXAnchor),
            stackView.centerYAnchor.constraint(equalTo: centerYAnchor),
            ])
        
        if __CGSizeEqualToSize(configuration.size, .zero) {
            NSLayoutConstraint.activate(NSLayoutConstraint.constraints(withVisualFormat: "H:|->=0-[stackView]->=0-|", options: [], metrics: nil, views: ["stackView" : stackView]))
            NSLayoutConstraint.activate(NSLayoutConstraint.constraints(withVisualFormat: "V:|->=0-[stackView]->=0-|", options: [], metrics: nil, views: ["stackView" : stackView]))
        } else {
            NSLayoutConstraint.activate(NSLayoutConstraint.constraints(withVisualFormat: "H:|->=0-[stackView(==metric)]->=0-|", options: [], metrics: ["metric" : configuration.size.width], views: ["stackView" : stackView]))
            NSLayoutConstraint.activate(NSLayoutConstraint.constraints(withVisualFormat: "V:|->=0-[stackView(==metric)]->=0-|", options: [], metrics: ["metric" : configuration.size.height], views: ["stackView" : stackView]))
        }
    }
}
